package cn.darkhorse.messystem.service.impl;

import cn.darkhorse.messystem.service.MesSysUserService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
public class MesSysUserServiceImpl implements MesSysUserService {
    @Resource
    private RedisCache redisCache;
    /**
     * @param userId
     * @return
     */
    @Override
    public boolean logOutForce(Long userId) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        boolean result=false;
        for (String key : keys){
            LoginUser user = redisCache.getCacheObject(key);
            if(user.getUserId().equals(userId)){
                result = redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + user.getToken());
            }
        }
        return result;
    }

//    /**
//     * @param company
//     * @return
//     */
//    @Override
//    public boolean logOutForceByCompany(int company) {
//        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
//        boolean result=false;
//        for (String key : keys){
//            LoginUser loginUser = redisCache.getCacheObject(key);
//            SysUser user = loginUser.getUser();
//            if(null!=user.getCompany()&&user.getCompany()==company){
//                result = redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + loginUser.getToken());
//            }
//        }
//        return result;
//    }

}
